﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using System.ComponentModel;
using FTP = FTPSClient;

namespace Vision.Core.ActivityComponents.IO.Ftp
{
    /// <summary>
    /// 
    /// </summary>
    [Designer(typeof(FtpUploadFileDataDesigner))]
    public class FtpUploadFileData : NativeActivity
    {
        internal FTP.FTPSClient FtpClient { get; set; }

        /// <summary>
        /// Gets or sets the file name to save as on the FTP site.
        /// </summary>
        [Category("Input")]
        public InArgument<string> RemoteFileName { get; set; }

        /// <summary>
        /// Gets or sets the date to upload to the FTP site.
        /// </summary>
        [Category("Input")]
        public InArgument<byte[]> FileData { get; set; }



        /// <summary>
        /// 
        /// </summary>
        /// <param name="context"></param>
        protected override void Execute(NativeActivityContext context)
        {
            this.FtpClient = (FTP.FTPSClient)context.Properties.Find("5AED2CE1-FB3C-45B3-9CB7-3DC8CD5E4C56");

            if (this.FtpClient != null)
            {
                byte[] data = this.FileData.Get(context);

                using (FTP.FTPStream stream = this.FtpClient.PutFile(context.GetValue(this.RemoteFileName)))
                {
                    stream.Write(data, 0, data.Length);
                }
            }
        }
    }
}
